﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HAP = HtmlAgilityPack;
using MSHTML = mshtml;
using mshtml;
using HtmlParsing.Helpers.Conditions;
using Helpers.Conditions;
using Helpers;

namespace HtmlParsing.Helpers
{
    public class HtmlParsingHelper
    {
        protected string Content { get; set; }
        protected HAP.HtmlDocument HapHtml { get; set; }
        protected MSHTML.IHTMLDocument2 MsHtml { get; set; }
        
        
        public HtmlParsingHelper(string content)
        {
            Content = content;
            
            HapHtml = new HtmlAgilityPack.HtmlDocument();
            HapHtml.LoadHtml(Content);

            MsHtml = new MSHTML.HTMLDocumentClass();
            MsHtml.write(Content);
            MsHtml.close();
        }


        public string GetTitle()
        {
            string result = null;
            
            HAP.HtmlNode node = HapHtml.DocumentNode.SelectSingleNode("/html/head/title");
            if (node != null)
                result = node.InnerText;

            return result;
        }

        public HAP.HtmlNode GetElementById(string elementId)
        {
            HAP.HtmlNode result = null;

            HAP.HtmlNode element = HapHtml.GetElementbyId(elementId);
            if (element != null)
                result = element;

            return result;
        }

        public List<MSHTML.IHTMLFormElement> GetForms(FormCondition condition)
        {
            return GetElements("FORM", condition);
        }

        public List<MSHTML.IHTMLAnchorElement> GetAnchors(AnchorCondition condition)
        {
            return GetElements("A", condition);
        }

        public List<MSHTML.IHTMLInputElement> GetInputs(InputCondition condition)
        {
            return GetElements("INPUT", condition);
        }

        public List<T> GetElements<T>(string tagName, BaseCondition<T> condition)
            where T : class
        {
            List<T> result = new List<T>();

            IHTMLElementCollection all = MsHtml.body.all as IHTMLElementCollection;
            foreach (IHTMLElement item in all)
            {
                if (item.tagName.ToUpper() != tagName)
                    continue;

                T element = item as T;

                if (condition.IsAccepted(element))
                    result.Add(element);
            }

            return result;
        }

        public List<MSHTML.IHTMLInputElement> GetInputsInForm(InputCondition inputCondition, FormCondition formCondition)
        {
            List<MSHTML.IHTMLInputElement> result = new List<IHTMLInputElement>();

            List<MSHTML.IHTMLInputElement> inputsList = GetInputs(inputCondition);

            if (CollectionHelper.IsNullOrEmpty(inputsList))
                return result;

            inputsList = inputsList.Where(input => formCondition.IsAccepted(input.form)).ToList();

            if (!CollectionHelper.IsNullOrEmpty(inputsList))
                result.AddRange(inputsList);

            return result;
        }
    }
}
